home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / recno / rec_delete.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  5.5 KB  |  205 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Mike Olson.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)rec_delete.c    8.2 (Berkeley) 9/7/93";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  42. #include "OurMalloc.h"
  43. #endif
  44.  
  45. #include <sys/types.h>
  46.  
  47. #include <errno.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50.  
  51. #include <db.h>
  52. #include "recno.h"
  53.  
  54. static int rec_rdelete __P((BTREE *, recno_t));
  55.  
  56. /*
  57.  * __REC_DELETE -- Delete the item(s) referenced by a key.
  58.  *
  59.  * Parameters:
  60.  *    dbp:    pointer to access method
  61.  *    key:    key to delete
  62.  *    flags:    R_CURSOR if deleting what the cursor references
  63.  *
  64.  * Returns:
  65.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  66.  */
  67. int
  68. __rec_delete(dbp, key, flags)
  69.     const DB *dbp;
  70.     const DBT *key;
  71.     u_int flags;
  72. {
  73.     BTREE *t;
  74.     recno_t nrec;
  75.     int status;
  76.  
  77.     t = dbp->internal;
  78.  
  79.     /* Toss any page pinned across calls. */
  80.     if (t->bt_pinned != NULL) {
  81.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  82.         t->bt_pinned = NULL;
  83.     }
  84.  
  85.     switch(flags) {
  86.     case 0:
  87.         if ((nrec = *(recno_t *)key->data) == 0)
  88.             goto einval;
  89.         if (nrec > t->bt_nrecs)
  90.             return (RET_SPECIAL);
  91.         --nrec;
  92.         status = rec_rdelete(t, nrec);
  93.         break;
  94.     case R_CURSOR:
  95.         if (!ISSET(t, B_SEQINIT))
  96.             goto einval;
  97.         if (t->bt_nrecs == 0)
  98.             return (RET_SPECIAL);
  99.         status = rec_rdelete(t, t->bt_rcursor - 1);
  100.         if (status == RET_SUCCESS)
  101.             --t->bt_rcursor;
  102.         break;
  103.     default:
  104. einval:        errno = EINVAL;
  105.         return (RET_ERROR);
  106.     }
  107.  
  108.     if (status == RET_SUCCESS)
  109.         SET(t, B_MODIFIED | R_MODIFIED);
  110.     return (status);
  111. }
  112.  
  113. /*
  114.  * REC_RDELETE -- Delete the data matching the specified key.
  115.  *
  116.  * Parameters:
  117.  *    tree:    tree
  118.  *    nrec:    record to delete
  119.  *
  120.  * Returns:
  121.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  122.  */
  123. static int
  124. rec_rdelete(t, nrec)
  125.     BTREE *t;
  126.     recno_t nrec;
  127. {
  128.     EPG *e;
  129.     PAGE *h;
  130.     int status;
  131.  
  132.     /* Find the record; __rec_search pins the page. */
  133.     if ((e = __rec_search(t, nrec, SDELETE)) == NULL) {
  134.         mpool_put(t->bt_mp, e->page, 0);
  135.         return (RET_ERROR);
  136.     }
  137.  
  138.     /* Delete the record. */
  139.     h = e->page;
  140.     status = __rec_dleaf(t, h, e->index);
  141.     if (status != RET_SUCCESS) {
  142.         mpool_put(t->bt_mp, h, 0);
  143.         return (status);
  144.     }
  145.     mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  146.     return (RET_SUCCESS);
  147. }
  148.  
  149. /*
  150.  * __REC_DLEAF -- Delete a single record from a recno leaf page.
  151.  *
  152.  * Parameters:
  153.  *    t:    tree
  154.  *    index:    index on current page to delete
  155.  *
  156.  * Returns:
  157.  *    RET_SUCCESS, RET_ERROR.
  158.  */
  159. int
  160. __rec_dleaf(t, h, index)
  161.     BTREE *t;
  162.     PAGE *h;
  163.     int index;
  164. {
  165.     register RLEAF *rl;
  166.     register indx_t *ip, offset;
  167.     register size_t nbytes;
  168.     register int cnt;
  169.     char *from;
  170.     void *to;
  171.  
  172.     /*
  173.      * Delete a record from a recno leaf page.  Internal records are never
  174.      * deleted from internal pages, regardless of the records that caused
  175.      * them to be added being deleted.  Pages made empty by deletion are
  176.      * not reclaimed.  They are, however, made available for reuse.
  177.      *
  178.      * Pack the remaining entries at the end of the page, shift the indices
  179.      * down, overwriting the deleted record and its index.  If the record
  180.      * uses overflow pages, make them available for reuse.
  181.      */
  182.     to = rl = GETRLEAF(h, index);
  183.     if (rl->flags & P_BIGDATA && __ovfl_delete(t, rl->bytes) == RET_ERROR)
  184.         return (RET_ERROR);
  185.     nbytes = NRLEAF(rl);
  186.  
  187.     /*
  188.      * Compress the key/data pairs.  Compress and adjust the [BR]LEAF
  189.      * offsets.  Reset the headers.
  190.      */
  191.     from = (char *)h + h->upper;
  192.     memmove(from + nbytes, from, (char *)to - from);
  193.     h->upper += nbytes;
  194.  
  195.     offset = h->linp[index];
  196.     for (cnt = &h->linp[index] - (ip = &h->linp[0]); cnt--; ++ip)
  197.         if (ip[0] < offset)
  198.             ip[0] += nbytes;
  199.     for (cnt = &h->linp[NEXTINDEX(h)] - ip; --cnt; ++ip)
  200.         ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
  201.     h->lower -= sizeof(indx_t);
  202.     --t->bt_nrecs;
  203.     return (RET_SUCCESS);
  204. }
  205.